export type NumberFormatOptionsType = {
  /**
   * @see https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat
   */
  localeMatcher?: string;
  /**
   * @see https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat
   */
  style?: string;
  /**
   * @see https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat
   */
  currency?: string;
  /**
   * @see https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat
   */
  currencyDisplay?: string;
  /**
   * @see https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat
   */
  currencySign?: string;
  /**
   * @see https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat
   */
  useGrouping?: boolean;
  /**
   * @see https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat
   */
  minimumIntegerDigits?: number;
  /**
   * @see https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat
   */
  minimumFractionDigits?: number;
  /**
   * @see https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat
   */
  maximumFractionDigits?: number;
  /**
   * @see https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat
   */
  minimumSignificantDigits?: number;

  /**
   * @see https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat
   */
  maximumSignificantDigits?: number;
} & Record<string, any>;

const defaultMoneyIntl = new Intl.NumberFormat('zh-Hans-CN', {
  currency: 'CNY',
  style: 'currency',
});

const enMoneyIntl = {
  style: 'currency',
  currency: 'USD',
};

const ruMoneyIntl = {
  style: 'currency',
  currency: 'RUB',
};

const rsMoneyIntl = {
  style: 'currency',
  currency: 'RSD',
};

const msMoneyIntl = {
  style: 'currency',
  currency: 'MYR',
};

const ptMoneyIntl = {
  style: 'currency',
  currency: 'BRL',
};

const intlMap = {
  default: defaultMoneyIntl,
  'zh-Hans-CN': {
    currency: 'CNY',
    style: 'currency',
  },
  'en-US': enMoneyIntl,
  'ru-RU': ruMoneyIntl,
  'ms-MY': msMoneyIntl,
  'sr-RS': rsMoneyIntl,
  'pt-BR': ptMoneyIntl,
};

export function getMoney(
  paramsText: number | string | undefined,
  precision: number,
  locale?: string | false,
  config?: NumberFormatOptionsType,
) {
  let moneyText: number | string | undefined = paramsText
    ?.toString()
    // @ts-ignore
    .replaceAll(',', '');
  if (typeof moneyText === 'string') {
    const parsedNum = Number(moneyText);
    // 转换数字为NaN时，返回原始值展示
    if (Number.isNaN(parsedNum)) return moneyText;
    moneyText = parsedNum;
  }

  if (!moneyText && moneyText !== 0) return '';

  try {
    // Formatting the number, when readonly moneySymbol = false, unused currency.
    const finalMoneyText = new Intl.NumberFormat(locale || 'zh-Hans-CN', {
      // @ts-ignore
      ...(intlMap[locale || 'zh-Hans-CN'] || intlMap['zh-Hans-CN']),
      maximumFractionDigits: precision,
      ...config,
    })
      // fix: #6003 解决未指定货币符号时，金额文本格式化异常问题
      .format(moneyText);

    // 是否有金额符号，例如 ￥ $
    const hasMoneySymbol = locale === false;

    /**
     * 首字母判断是否是正负符号
     */
      // @ts-ignore
    const [operatorSymbol] = finalMoneyText || '';

    // 兼容正负号
    if (['+', '-'].includes(operatorSymbol)) {
      // 裁剪字符串,有符号截取两位，没有符号截取一位
      return `${operatorSymbol}${finalMoneyText.substring(
        hasMoneySymbol ? 2 : 1,
      )}`;
    }

    // 没有正负符号截取一位
    return finalMoneyText.substring(hasMoneySymbol ? 1 : 0) + `${config?.suffix || ''}`;
  } catch (error) {
    console.warn('数字转换错误', error);
    return moneyText;
  }
}
